'''DataK protocol package

 This package contains a serie of usefull protocols in its modules.
 
 A DataK protocol is responsible of the creation of the strategies used
by items under the connection it belongs to.

 When creating a DataK server, the protocol configuration is stored in it.
When a connection is later created on it, this configuration is used to
instanciate a corresponding protocol.

 See the module _base if you want to create your own protocol.
  
# LICENSE ....................................................................
    This file is part of DataK.

    DataK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DataK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DataK.  If not, see <http://www.gnu.org/licenses/>.
# ............................................................................

'''

def create_protocol( user, protocol_definition=None ):
    '''
    Returns the protocol described by the string protocol_definition,
    configured to work for user. 
    
    Returns the default protocol if protocol_definition is None.
    
    See also get_definition().
    '''
    if protocol_definition is None:
        import ez
        ret = ez.EZ()
        ret.user = user
        return ret
        
    if '.' in protocol_definition:
        module = protocol_definition.rsplit('.',1)[0]
        eval "import %s; ret = %s" % (module, protocol_definition)
    else:
        eval "ret = %s" % protocol_definition
    
    ret.user = user
    
    return ret
    
def get_definition( protocol ):
    '''
    Returns a string descibing the given protocol.
    
    See also create_protocol().
    '''
    # TODO: this is kool looking but not efficient, we need to store config
    # separatly in order to have the user in the constructor in create_protocol.
    return str(protocol.__class__)+'('+protocol.config_string()+')'


